<template>
  <div id="login">
    <div class="formbox">
      <img src="../assets/img/pic/logo/orange.png" alt="" class="logo" />
      <input type="text" placeholder="手机号" class="text" v-model="inputTel" />
      <input type="text" placeholder="昵称" class="text" v-model="inputNick" />
      <input type="text" placeholder="密码" class="text" v-model="inputPw" />
      <button class="btn" @click="doregister" :class="flag">注册</button>
    </div>
  </div>
</template>

<script>
import { Register } from "../util/request/api";
import { Dialog } from "vant";
export default {
  data() {
    return {
      inputTel: "",
      inputPw: "",
      inputNick: "",
      flag: "",
    };
  },
  watch: {
    // 判断改变登录按钮颜色
    inputPw: function () {
      this.onchange();
    },
    inputTel: function () {
      this.onchange();
    },
  },
  methods: {
    doregister() {
      if (this.inputTel == "" || this.inputNick == "" || this.inputPw == "") {
        Dialog({ message: "请确认全部填写完成" });
        return;
      }
      // axios
      Register({
        phone: this.inputTel,
        nickname: this.inputNick,
        password: this.inputPw,
      })
        .then((res) => {
          if (res.code == 200) {
            Dialog({ message: "注册成功" });
            this.$router.push("/login");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onchange() {
      if (this.inputTel != "" && this.inputPw != "" && this.inputNick != "") {
        this.flag = "active";
      } else {
        this.flag = "";
      }
    },
  },
};
</script>

<style scoped>
@import url("../assets/css/login.css");
</style>